package com.ksy.media.widget.util.log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.util.Log;

import com.ksy.media.widget.util.Constants;

public class Cpu {
	static final String USER_CPU_REGEX = "((?i)(use?r\\s+[0-9]{1,2}(\\.[0-9]{1,2})?\\%))|([0-9]{1,2}\\.[0-9]{1,2}\\%\\s+(?i)(use?r))";
	static final String SYSTEM_CPU_REGEX = "((?i)(sys(tem)?\\s+[0-9]{1,2}(\\.[0-9]{1,2})?\\%))|([0-9]{1,2}\\.[0-9]{1,2}\\%\\s+(?i)(sys(tem)?))";

	private String m_sTopResults;

	/**
	 * Store the user CPU usage
	 */
	private float m_fUserCpuUsage;
	/**
	 * Store the system CPU usage
	 */
	private float m_fSystemCpuUsage;
	/**
	 * Store the idle CPU
	 */
	private float m_fIdleCpuUsage;

	public String m_sProcessCpuUsage;
	
	private String m_Package;

	public Cpu() {
		m_sTopResults = null;
		m_fIdleCpuUsage = 0f;
		m_fSystemCpuUsage = 0f;
		m_fUserCpuUsage = 0f;
	}
	
	public Cpu(String packa) {
		m_sTopResults = null;
		m_fIdleCpuUsage = 0f;
		m_fSystemCpuUsage = 0f;
		m_fUserCpuUsage = 0f;
		
		m_Package = packa;
	}

	private void getCPUInfo() {
		BufferedReader ifp = null;

		// * empty the raw results buffer
		this.m_sTopResults = "";

		try {
			// * execute the top command
			Process process = null;
			process = Runtime.getRuntime().exec("top -n 1 -d 1"); //
			// * we only want to retrieve the top few lines
			// * read the output from the command
			String sLine = new String();
			ifp = new BufferedReader(new InputStreamReader(
					process.getInputStream()));

			// * Read all the available output and store it in the class member
			if (m_Package != null) {
				while ((sLine = ifp.readLine()) != null) {
					// get data from 'top -n 1 -d 1' ,you should parse the result
					// using keyword of your process package name
					if (sLine.indexOf(m_Package) >= 0) {
						this.m_sTopResults = sLine;
						break;
					}
				}
			} else {
				
				Log.e(Constants.LOG_TAG, "m_Package is  null ");
			}
			
		} catch (IOException exp) {
			Log.e(Constants.LOG_TAG, "exp 11 = " + exp);
		} finally {
			// * we need to close the stream once everything is done.
			try {
				if (ifp != null)
					ifp.close();
			} catch (IOException exp) {
				Log.e(Constants.LOG_TAG, "exp 22 = " + exp);
			}
		}
	}

	public void parseTopResults() {
//		String sUserCpuUsage = null;
//		String sSystemCpuUsage = null;
//
//		Pattern xRegexSearchPattern = null;
//		Matcher xSearch = null;

		getCPUInfo();

		// * Check to make sure we have some data to parse
		if (this.m_sTopResults == null) {
			return;
		}

		String[] result = m_sTopResults.split(" ");
		if (result != null && result.length > 0) {
			for (String idx : result) {
				if (idx.indexOf("%") > 0) {
					m_sProcessCpuUsage = idx;
					m_sProcessCpuUsage = m_sProcessCpuUsage.replaceAll("%", "");
					break;
				}
			}
		}

		// //* search for the user CPU information
		// xRegexSearchPattern = Pattern.api(Cpu.USER_CPU_REGEX);
		// xSearch = xRegexSearchPattern.matcher(this.m_sTopResults);
		// //* check to see if we found a match
		// if (xSearch.find())
		// {
		// sUserCpuUsage = xSearch.group(0);
		//
		// //* we want to remove the non numeric characters in the string
		// try {
		// this.m_fUserCpuUsage = Float.parseFloat( sUserCpuUsage.replaceAll(
		// "[^\\d.]", "" ).trim() );
		// }
		// catch (NumberFormatException exp)
		// {
		// this.m_fUserCpuUsage = 0.0F;
		// }
		// }
		//
		// //* search for the system CPU information
		// xRegexSearchPattern = Pattern.compile( Cpu.SYSTEM_CPU_REGEX );
		// xSearch = xRegexSearchPattern.matcher(this.m_sTopResults);
		// //* check to see if we found a match
		// if (xSearch.find())
		// {
		// sSystemCpuUsage = xSearch.group(0);
		//
		// //* we want to remove the non numeric characters in the string
		// try {
		// this.m_fSystemCpuUsage = Float.parseFloat(
		// sSystemCpuUsage.replaceAll( "[^\\d.]", "" ).trim() );
		// }
		// catch (NumberFormatException exp)
		// {
		// this.m_fUserCpuUsage = 0.0F;
		// }
		//
		// }
		//
		// //* now we compute the idle CPU usage based on the other two
		// this.m_fIdleCpuUsage = 100.0F - this.m_fSystemCpuUsage -
		// this.m_fUserCpuUsage;
	}

	public String summaryString() {
		String sSummary = "";

		sSummary += "CPU Information: \n";
		sSummary += "User CPU utilized: " + this.m_fUserCpuUsage + "%\n";
		sSummary += "System CPU utilized: " + this.m_fSystemCpuUsage + "%\n";
		sSummary += "Idle CPU: " + this.m_fIdleCpuUsage + "%\n";

		return sSummary;
	}

	/**
	 * Return the percent of the CPU utilized by the system processes.
	 * 
	 * @return The percentage of CPU utilized by system processes.
	 */
	public float getSystemUsage() {
		return this.m_fSystemCpuUsage;
	}

	/**
	 * Return the percent of the CPU that is being utilized by user processes.
	 * 
	 * @return The percentage of CPU utilized by user processes.
	 */
	public float getUserUsage() {
		return this.m_fUserCpuUsage;
	}

	/**
	 * Return the percentage of the CPU that is idle
	 * 
	 * @return The percentage of idle CPU
	 */
	public float getIdle() {
		return this.m_fIdleCpuUsage;
	}

	public String getProcessCpuUsage() {
		return this.m_sProcessCpuUsage;
	}
}
